/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.struts.dao;

/**
 *
 * @author Higor
 * DAO de inserir e de apagar registros do BD
 */

import java.util.List;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;

public abstract class DAOGenerics <K,G>{

    //insere qualquer tipo de objeto
    public void inserir(K obj){

        SessionFactory sf = new AnnotationConfiguration().configure().buildSessionFactory();
        Session s = sf.openSession();

        s.getTransaction().begin();

        s.saveOrUpdate(obj);
        s.getTransaction().commit();
        s.close();
    }

    //apaga qualquer tipo de objeto
    public void apagar(K obj, Session s) {

        s.getTransaction().begin();
        s.delete(obj);
        s.getTransaction().commit();

    }

    public void apagar(K obj) {

        SessionFactory sf = new AnnotationConfiguration().configure().buildSessionFactory();
        Session s = sf.openSession();

        try {
            apagar(obj, s);
        } finally {
            s.close();
        }
    }

    public abstract K buscar(G obj);

    public abstract K buscar(G obj, Session s);

    public abstract List<K> buscarTodos();

    public abstract List<K> buscarTodos(Session s);
}


